package com.heima.model.media.dtos;

import com.heima.common.util.JsonUtils;
import io.swagger.models.auth.In;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Data
public class WmNewsContentDto {
    private String type;
    private String value;
    private Integer id;

    private final static String TYPE_IMAGE = "image";
    private final static String TYPE_TEXT = "text";

    /**
     * 返回一个json串
     * @param content
     * @return
     */
    public static List<WmNewsContentDto> parseContent(String content){
        if (StringUtils.isEmpty(content)){
            return null;
        }
        return JsonUtils.toList(content,WmNewsContentDto.class);
    }

    /**
     * 回去图片的路径
     * @param content
     * @return
     */
    public static List<WmNewsContentDto> getImages(String content){
        List<WmNewsContentDto> newsContentDtoList = parseContent(content);
        if (CollectionUtils.isEmpty(newsContentDtoList)){
            return null;
        }
        return newsContentDtoList.stream().filter(WmNewsContentDto::isImageItem)
                .collect(Collectors.toList());

    }

    /**
     * 获取文本
     * @param content
     * @return
     */
    public static List<WmNewsContentDto> getText(String content){
        List<WmNewsContentDto> newsContentDtoList = parseContent(content);
        if (CollectionUtils.isEmpty(newsContentDtoList)){
            return null;
        }
        return newsContentDtoList.stream().filter(WmNewsContentDto::isTextItem)
                .collect(Collectors.toList());
    }

    public static boolean isImageItem(WmNewsContentDto item){
        return item.getType().equals(TYPE_IMAGE);
    }

    public static boolean isTextItem(WmNewsContentDto item){
        return item.getType().equals(TYPE_TEXT);
    }

}
